package token.mobileapp.controlactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import token.mobileapp.R;
import token.mobileapp.TokenActivity;
import token.mobileapp.model.UserInfo;
import token.mobileapp.persistenceactivity.UserInfoPersistence;
import token.mobileapp.utils.Constants;

/**
 * token.mobileapp.controlactivity
 * Author: AVAVT
 * Date: 7/13/11
 * Time: 10:38 PM
 */
public class ChangePIN extends TokenActivity implements View.OnClickListener {
    private Button buttonBack, buttonSubmit;
    private EditText oldPIN, newPIN, rePIN;
    private UserInfo userInfo;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_pin);
        buttonBack = (Button) findViewById(R.id.PIN_btn_back);
        buttonBack.setOnClickListener(this);
        buttonSubmit = (Button) findViewById(R.id.PIN_btn_submit);
        buttonSubmit.setOnClickListener(this);
        oldPIN = (EditText) findViewById(R.id.PIN_et_oldPIN);
        newPIN = (EditText) findViewById(R.id.PIN_et_newPIN);
        rePIN = (EditText) findViewById(R.id.PIN_et_rePIN);

        Intent intent = new Intent(this, UserInfoPersistence.class);
        startActivityForResult(intent, Constants.REQUEST_CODE_LOAD_USER);
    }

    public void onClick(View view) {
        if (view.getId() == R.id.PIN_btn_back) {
            finish();
        }
        else if (view.getId() == R.id.PIN_btn_submit) {
            if(oldPIN.getText().toString().compareTo(userInfo.getPINCode()) != 0){
                displayAlert("Incorrect old PIN code");
            }
            else if (newPIN.getText().toString().compareTo("") == 0){
                displayAlert("Please enter a PIN code.");
            }
            else if (rePIN.getText().toString().compareTo("") == 0){
                displayAlert("Please re-enter your new PIN code.");
            }
            else if(newPIN.getText().toString().compareTo(rePIN.getText().toString()) != 0){
                displayAlert("PIN codes don't match.");
            }
            else if(newPIN.getText().toString().length() != 6){
                displayAlert("PIN code must be a 6-digits number.");
            }
            else{
                buttonSubmit.setClickable(false);

                userInfo.setPINCode(newPIN.getText().toString());

                Intent intent = new Intent(this, UserInfoPersistence.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("userInfo", userInfo);
                intent.putExtras(bundle);

                startActivityForResult(intent, Constants.REQUEST_CODE_SAVE_USER);
            }
        }
    }

     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constants.REQUEST_CODE_LOAD_USER && resultCode == Activity.RESULT_OK) {
            Bundle bundle = data.getExtras();
            userInfo = (UserInfo) bundle.getSerializable("userInfo");
        } else if (requestCode == Constants.REQUEST_CODE_LOAD_USER && resultCode == Activity.RESULT_CANCELED) {
            displayError(data);
        } else if (requestCode == Constants.REQUEST_CODE_SAVE_USER && resultCode == Activity.RESULT_OK) {
            buttonSubmit.setClickable(true);
            displayAlertThenFinish("PIN code changed successfully.");
        } else if (requestCode == Constants.REQUEST_CODE_SAVE_USER && resultCode == Activity.RESULT_CANCELED) {
            displayError(data);
            buttonSubmit.setClickable(true);
        }
    }
}
